package com.ruoyi.web.controller.substation;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.system.domain.SProtectDevice;
import com.ruoyi.system.domain.SSubstationDevice;
import com.ruoyi.system.service.ISSubstationDeviceService;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SDeviceVoltageLevel;
import com.ruoyi.system.service.ISDeviceVoltageLevelService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 设备电压等级Controller
 * 
 * @author ruoyi
 * @date 2023-07-05
 */
@Api(tags = "设备电压等级管理")
@RestController
@RequestMapping("/substation/voltageLevel")
public class SDeviceVoltageLevelController extends BaseController
{
    @Autowired
    private ISDeviceVoltageLevelService sDeviceVoltageLevelService;

    @Autowired
    private ISSubstationDeviceService substationDeviceService;

    /**
     * 查询设备电压等级列表
     */
    @ApiOperation("获取设备电压等级列表")
    @PreAuthorize("@ss.hasPermi('substation:voltageLevel:list')")
    @GetMapping("/list")
    public TableDataInfo list(SDeviceVoltageLevel sDeviceVoltageLevel)
    {
        startPage();
        List<SDeviceVoltageLevel> list = sDeviceVoltageLevelService.list(sDeviceVoltageLevel.queryWrapper());
        return getDataTable(list);
    }

    /**
     * 导出设备电压等级列表
     */
    @ApiOperation("导出设备电压等级列表")
    @PreAuthorize("@ss.hasPermi('substation:voltageLevel:export')")
    @Log(title = "设备电压等级", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SDeviceVoltageLevel sDeviceVoltageLevel)
    {
        List<SDeviceVoltageLevel> list = sDeviceVoltageLevelService.list(sDeviceVoltageLevel.queryWrapper());
        ExcelUtil<SDeviceVoltageLevel> util = new ExcelUtil<SDeviceVoltageLevel>(SDeviceVoltageLevel.class);
        util.exportExcel(response, list, "设备电压等级数据");
    }

    /**
     * 导入设备电压等级列表
     */
    @ApiOperation("导入设备电压等级")
    @PreAuthorize("@ss.hasPermi('substation:voltageLevel:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        String message = "操作成功";
        try {
            message = sDeviceVoltageLevelService.importData(file);
        }catch (IllegalStateException e){
            throw new ServiceException("请检查表格格式是否为文本格式!");
        }
        return success(message);
    }


    /**
     * 获取设备电压等级详细信息
     */
    @ApiOperation("获取设备电压等级详细")
    @ApiImplicitParam(name = "id", value = "设备电压等级ID", required = true, dataType = "String", paramType = "path")
    @PreAuthorize("@ss.hasPermi('substation:voltageLevel:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(sDeviceVoltageLevelService.getById(id));
    }

    /**
     * 新增设备电压等级
     */
    @ApiOperation("新增设备电压等级")
    @PreAuthorize("@ss.hasPermi('substation:voltageLevel:add')")
    @Log(title = "设备电压等级", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SDeviceVoltageLevel sDeviceVoltageLevel)
    {
        sDeviceVoltageLevelService.checkDeviceVoltageLevel(sDeviceVoltageLevel);
        return toAjax(sDeviceVoltageLevelService.save(sDeviceVoltageLevel));
    }

    /**
     * 修改设备电压等级
     */
    @ApiOperation("更新设备电压等级")
    @PreAuthorize("@ss.hasPermi('substation:voltageLevel:edit')")
    @Log(title = "设备电压等级", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SDeviceVoltageLevel sDeviceVoltageLevel)
    {
        SDeviceVoltageLevel byId = sDeviceVoltageLevelService.getById(sDeviceVoltageLevel.getId());
        if(!sDeviceVoltageLevel.getDeviceVoltageLevel().equals(byId.getDeviceVoltageLevel())) {
            sDeviceVoltageLevelService.checkDeviceVoltageLevel(sDeviceVoltageLevel);
        }
        return toAjax(sDeviceVoltageLevelService.updateById(sDeviceVoltageLevel));
    }

    /**
     * 删除设备电压等级
     */
    @ApiOperation("删除设备电压等级")
    @ApiImplicitParam(name = "ids", value = "设备电压等级ID集合", required = true, dataType = "String", paramType = "path")
    @PreAuthorize("@ss.hasPermi('substation:voltageLevel:remove')")
    @Log(title = "设备电压等级", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<String> ids)
    {
        for (String id : ids) {
            long count = substationDeviceService.count(new LambdaQueryWrapper<SSubstationDevice>()
                    .eq(SSubstationDevice::getVoltageLevel, id));
            if(count > 0){
                throw new ServiceException("该电压等级已存在设备！");
            }
        }
        return toAjax(sDeviceVoltageLevelService.removeByIds(ids));
    }
}
